package fr.istia.gesstages.metier;

import fr.istia.gesstages.dao.IDaoApply;
import fr.istia.gesstages.dao.IDaoOffer;
import fr.istia.gesstages.dao.IDaoUser;
import fr.istia.gesstages.dao.IDaoUserType;
import fr.istia.gesstages.jpa.Apply;
import fr.istia.gesstages.jpa.Offer;
import fr.istia.gesstages.jpa.User;
import fr.istia.gesstages.jpa.UserType;
import fr.istia.gesstages.util.Access;
import java.util.HashMap;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class Metier implements IMetier {

    private IDaoOffer daoOffer;
    private IDaoApply daoApply;
    private IDaoUser daoUser;
    private IDaoUserType daoUserType;
    private Access access;

    public User findUserByLogin(String login) {
        return daoUser.findByLogin(login);
    }

    public List<User> findUsersByType(UserType type) {
        return daoUser.findByType(type);
    }

    public UserType findUserTypeByName(String name) {
        return daoUserType.findByName(name);
    }

    public List<Apply> findAppliesByUser(User user) {
        return daoApply.findByUser(user);
    }

    public List<Apply> findAppliesByOffer(Offer offer) {
        return daoApply.findByOffer(offer);
    }

    public List<Offer> findAllOffers() {
        return daoOffer.findAll();
    }

    public List<Apply> findAllApplies() {
        return daoApply.findAll();
    }

    public List<User> findAllUsers() {
        return daoUser.findAll();
    }

    public List<UserType> findAllUserTypes() {
        return daoUserType.findAll();
    }

    public boolean login(String login, String password) {
        return access.login(login, password);
    }

    public void deleteAllOffers() {
        daoOffer.deleteAll();
    }

    public void deleteAllApplies() {
        daoApply.deleteAll();
    }

    public HashMap<User, List<Apply>> findAppliesForEachUser() {
        List<User> allUsers = this.findAllUsers();
        HashMap<User, List<Apply>> stagesParUser = new HashMap<User, List<Apply>>();
        for (User u : allUsers) {
            stagesParUser.put(u, daoApply.findByUser(u));
        }
        return stagesParUser;
    }

    public Apply createApply(Apply a) {
        return daoApply.create(a);
    }

    public User createUser(User u) {
        return daoUser.create(u);
    }

    public Offer createOffer(Offer o) {
        return daoOffer.create(o);
    }

    //-------------------------------------------------------------
    //------------------- Getters // Setters ----------------------
    @WebMethod(exclude = true)
    public void setDaoApply(IDaoApply daoApply) {
        this.daoApply = daoApply;
    }

    @WebMethod(exclude = true)
    public void setDaoOffer(IDaoOffer daoOffer) {
        this.daoOffer = daoOffer;
    }

    @WebMethod(exclude = true)
    public void setDaoUser(IDaoUser daoUser) {
        this.daoUser = daoUser;
    }

    @WebMethod(exclude = true)
    public void setDaoUserType(IDaoUserType daoUserType) {
        this.daoUserType = daoUserType;
    }

    @WebMethod(exclude = true)
    public void setAccess(Access access) {
        this.access = access;
    }
    //-------------------------------------------------------------

    public User findUserById(int id) {
        return daoUser.find(id);
    }

    public UserType findUserTypeById(int id) {
        return daoUserType.find(id);
    }

    public Offer findOfferById(int id) {
        return daoOffer.find(id);
    }

    public Apply findApplyByOfferAndUser(Offer o, User u) {
        return daoApply.findBy(o, u);
    }

    public Apply updateApply(Apply a) {
        return daoApply.update(a);
    }

    public User updateUser(User u) {
        return daoUser.update(u);
    }

    public Offer updateOffer(Offer o) {
        return daoOffer.update(o);
    }

    public void deleteApply(Apply a) {
        daoApply.delete(a);
    }

    public void deleteUser(User u) {
        daoUser.delete(u);
    }

    public void deleteOffer(Offer o) {
        daoOffer.delete(o);
    }

    public List<Offer> findOffersNotAppliedBy(User u) {
        return daoOffer.findNotAppliedBy(u);
    }

    public HashMap<Offer, List<Apply>> findAppliesForEachOffer() {
        List<Offer> offers = daoOffer.findAll();
        HashMap<Offer, List<Apply>> appliesByOffer = new HashMap<Offer, List<Apply>>();
        for(Offer o : offers)
            appliesByOffer.put(o, daoApply.findByOffer(o));
        return appliesByOffer;
    }

    public HashMap<User, List<Apply>> findAppliesForEachUserOfType(UserType type) {
        List<User> allUsers = this.findUsersByType(type);
        HashMap<User, List<Apply>> stagesParUser = new HashMap<User, List<Apply>>();
        for (User u : allUsers) {
            stagesParUser.put(u, daoApply.findByUser(u));
        }
        return stagesParUser;
    }

    public List<Offer> findOffersNotAttributed() {
        return daoOffer.findNotAttributed();
    }

    public Apply findApplyById(int id) {
        return daoApply.findBy(id);
    }

    public List<Offer> findOffersNotArchived() {
        return daoOffer.findCurrent();
    }

    public List<Offer> findOffersArchived() {
        return daoOffer.findArchived();
    }
}
